package com.breeze.leetcode.easy;

import java.util.Arrays;

/**
 * @url <a href="https://leetcode.cn/problems/maximum-number-of-balls-in-a-box">1742. 盒子中小球的最大数量</a>
 * @author yangzh
 * @date 2025/2/13
 */
public class T1742MaximumNumberOfBallsInBox {
    public static void main(String[] args) {
        T1742MaximumNumberOfBallsInBox solution = new T1742MaximumNumberOfBallsInBox();
        int result = solution.countBalls(5, 1517);
        System.out.println(result);
    }

    public int countBalls(int lowLimit, int highLimit) {
        int[] nums = new int[46];
        Arrays.fill(nums, 0);

        int max = nums[0];
        for (int i = lowLimit; i <= highLimit; i++) {
            max = Math.max(++nums[calculate(i)], max);
        }
        return max;
    }

    public int calculate(int num) {
        if (num < 10) return num;

        String numStr = String.valueOf(num);
        int sum = 0;
        for (char c : numStr.toCharArray()) {
            sum += c - '0';
        }
        return sum;
    }
}
